<?php

namespace App\Admin\Controllers;

use App\Models\Tool\Notice;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class NoticeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Notice';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Notice());

        $grid->column('id', __('ID'));
        $grid->column('uid', __('用户ID'))->display(function ($value) {
            if ($value == 0) {
                return "系统管理";
            }
            return $value;
        });
        $grid->column('content', __('内容'));
        $states = [
            'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
        ];
        $grid->column('state', __('状态'))->switch($states);
        $grid->column('create_time', __('创建时间'));
        $grid->column('update_time', __('更新时间'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Notice::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('uid', __('Uid'));
        $show->field('content', __('Content'));
        $show->field('state', __('State'));
        $show->field('is_del', __('Is del'));
        $show->field('create_time', __('Create time'));
        $show->field('update_time', __('Update time'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Notice());

        $form->number('uid', __('用户ID'));
        $form->textarea('content', __('内容'));
        $form->switch('state', __('状态'));

        return $form;
    }
}
